package com.hbnu.net;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;

/**
 * @author kittychen
 * @since 2025-10-30 19:37
 */
public class UdpSendDemo02 {

    public static void main(String[] args) {
        DatagramSocket datagramSocket = null;

        try {
            datagramSocket = new DatagramSocket();

            BufferedReader bufferedReader= new BufferedReader(new InputStreamReader(System.in));  // 创建键盘输入流

            String line = null;
            while((line = bufferedReader.readLine()) != null) {
                if("886".equals(line)) break;
                byte[] data = line.getBytes();
                DatagramPacket datagramPacket = new DatagramPacket(data, data.length,
                        InetAddress.getByName("127.0.0.1"), 8888);
                datagramSocket.send(datagramPacket);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            if (datagramSocket != null)
                datagramSocket.close();
        }
    }
}
